package com.rs2lite.utils;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.Random;

import com.rs2lite.RS2Lite;

public class AutoClicker {
	
	private long delay = 1500;
	
	private Random random = new Random();
	
	private Robot robot;
	
	public AutoClicker() {
		try {
			robot = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}
	
	private boolean running = false;
	
	public void start() {
		this.running = true;
		new Thread(new AutoClickerRunnable()).start();
	}
	
	public void stop() {
		this.running = false;
	}
	
	public class AutoClickerRunnable implements Runnable {
		@Override
		public void run() {
			while(running) {
				checkPause();
				if(!running)
					break;
				robot.mousePress(InputEvent.BUTTON1_MASK);
				robot.mouseRelease(InputEvent.BUTTON1_MASK);
				try {
					Thread.sleep(random.nextInt(200));
				} catch(Exception e) {}
				robot.mousePress(InputEvent.BUTTON1_MASK);
				robot.mouseRelease(InputEvent.BUTTON1_MASK);
				try {
					Thread.sleep(delay + random.nextInt((int) (delay / 4)));
				} catch(Exception e) {}
			}
		}
		
		private void checkPause() {
			if (RS2Lite.getAutoTyper().isPaused()) {
				while (RS2Lite.getAutoTyper().isPaused()) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	public boolean isRunning() {
		return running;
	}
}
